package com.xunk.common.core.wrapper;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.equipment.wrapper
 * @ClassName: ClientHttpResponseWrapper
 * @Author: luanzhiwei
 * @Description: 返回包装类
 * @Date: 2021/4/27 9:38
 * @Version: 1.0
 */
public class ClientHttpResponseWrapper implements ClientHttpResponse {

    private ClientHttpResponse response;

    private byte[] body;

    public ClientHttpResponseWrapper(ClientHttpResponse response) {
        this.response = response;
    }
    
    @Override
    public HttpStatus getStatusCode() throws IOException {
        return this.response.getStatusCode();
    }

    @Override
    public int getRawStatusCode() throws IOException {
        return this.response.getRawStatusCode();
    }

    @Override
    public String getStatusText() throws IOException {
        return this.response.getStatusText();
    }

    @Override
    public HttpHeaders getHeaders() {
        return this.response.getHeaders();
    }

    @Override
    public InputStream getBody() throws IOException {
        if (this.body == null) {
            this.body = StreamUtils.copyToByteArray(this.response.getBody());
        }
        return new ByteArrayInputStream(this.body);
    }

    @Override
    public void close() {
        this.response.close();
    }
}
